package Reflect;

import Reflect.annotations.AutoRunClass;
import Reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test4 {
    public static void main(String[] args) throws Exception {

        File dir = new File(Test4.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));

        for (File sub:subs){
            String name = sub.getName();
//            System.out.println(name);
            String[] split = name.split("\\.");
            String path = Test4.class.getPackage().getName() + "." + split[0];
//            System.out.println(path);
            Class cls = Class.forName(path);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                Object o = cls.newInstance();
                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods){
                    if (method.isAnnotationPresent(AutoRunMethod.class)){
                        System.out.println(method.getName() + "被注解@AutoRunMethod标记了");
                        method.invoke(o);
                    }
                }
            }


        }

    }

}
